'''
Created on 12-12-2012

@author: Maryla
'''
from Info import VMInfo
from Logger import logger  
from vboxapi import VirtualBoxManager
import sys


class VMAdmin:
    
    def __init__(self):
        pass
    
    def startVM(self, nameOrId, type='gui', vmInfo=None):
        mgr = VirtualBoxManager(None, None)
        sessionMgr = mgr.mgr
        vbox = mgr.vbox
        mach = vbox.findMachine(nameOrId)
        session =sessionMgr.getSessionObject(vbox)
        progress = mach.launchVMProcess(session, type, '')
        progress.waitForCompletion(-1)
            
            
    def stopVM(self, nameOrId):
        vbox = VirtualBoxManager(None, None)
        virtualbox = vbox.getVirtualBox()
        machine = virtualbox.findMachine(nameOrId)
        mgr = vbox.mgr
        session = mgr.getSessionObject(vbox.getVirtualBox())
        machine.lockMachine(session, 1)
        console = session.console
#        console.powerButton()  #this won't raise anything, but the VM won't shut down
        console.powerDown() 
            
            
if __name__ == "__main__":
        adm = VMAdmin()
        adm.startVM('fedora');
#        adm.stopVM('fedora')
        
    